package Stack._225_ImplementStackusingQueues;

import java.util.LinkedList;
import java.util.Queue;

public class MyStack {
    /** Initialize your data structure here. */
    Queue<Integer> q1;
    Queue<Integer> q2;
    int first;
    public MyStack() {
        q1 = new LinkedList<>();
        q2 = new LinkedList<>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        if (q1.isEmpty()) {
            first = x;
        }
        q2.add(x);
        first = x;
        while (!q1.isEmpty()) {
            q2.add(q1.remove());
        }
        q1 = q2;
        q2 = new LinkedList<>();
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        int x =  q1.remove();
        if (!q1.isEmpty()) {
            first = q1.peek();

        }
        return x;
    }

    /** Get the top element. */
    public int top() {
        return first;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return q1.isEmpty();
    }
}
